<?php
 /*
  *总后台活动产品模型类
  */
namespace Home\Model; 
use Think\Model;
class  ActiviProductModel
{
	/*
	 * 活动产品列表
	 */
	public function index($arg=array())
	{
		
		$page=!empty($arg['page'])?intval($arg['page']):1;  
		$pid=!empty($arg['pid'])?intval($arg['pid']):0;  
		$cid=!empty($arg['cid'])?intval($arg['cid']):0;  

		$condition='1=1';

		if (!empty($pid)) 
		{
			$condition.=' AND pid='.$pid;
		}

		if (!empty($cid))
		{
			$condition.=' AND cid='.$cid;
		}



		$activity=M('activity a');
		$result='';
		//获取所有活动产品
		$activ_item=M('activ_item');

		$count=M()->query("select count(*) as count from ".C(DB_PREFIX).'activ_item where '.$condition);
 		

 		$pagearr= getPageArr($page,C('PAGESIZE'),$count[0]['count']);


		$temp=$activ_item->where($condition)
						 ->order('id desc')
						 ->limit($pagearr['start'],$pagearr['pagesize'])
						 ->select();

		if (!empty($temp)) 
		{
			$item=M('item');
			$product=M('product');
			foreach ($temp as $key => $value)
			{
				$result[$key]['id']=$value['id'];
				$result[$key]['activ_price']=$value['activ_price'];
				$result[$key]['activ_num']=$value['activ_num']; //参与活动产品数量
				$result[$key]['limit_num']=$value['limit_num'];//限购数量
				$result[$key]['res_inventory']=$value['res_inventory'];//剩余活动产品数量
				//获取子主题，父主题
				$result[$key]['pname']=$activity->where('id=%d',$value['pid'])->getField('parenttheme');
				$result[$key]['cname']=$activity->where('id=%d',$value['cid'])->getField('childtheme');
				//获取此产品名称，型号
				$result[$key]['itemprop']=$item->where('itemid=%d',$value['itemid'])->getField('itemname');//产品名称
				$result[$key]['itemprop'].=$item->where('itemid=%d',$value['itemid'])->getField('bn');   //产品名称+型号
				//判断此产品是否有规格
				if (!empty($value['productid'])) 
				{
					$result[$key]['itemprop'].=$product->where('productid=%d',$value['productid'])->getField('color');
				}

			}
		}

		$tem['items']=$result;
		$tem['pagearr']=$pagearr;

		return $tem;
	}

	/*
 	*ajax 修改活动产品
 	*param id 活动产品ID
 	*return json
 	*/
 	public function editProduct($arg='')
 	{
 		$result='';
 		$id=!empty($arg)?intval($arg):0;

 		$activ_item=M('activ_item');
 		$temp=$activ_item->where('id=%d',$id)->select();
		if (!empty($temp)) 
		{
			$item=M('item');
			$product=M('product');
			$activity=M('activity a');
			$tempselect='';
			$cnameselect='';
			foreach ($temp as $key => $value)
			{
				$result[$key]['id']=$value['id'];
				$result[$key]['activ_price']=$value['activ_price'];
				$result[$key]['activ_num']=$value['activ_num']; //参与活动产品数量
				$result[$key]['limit_num']=$value['limit_num'];//限购数量
				$result[$key]['res_inventory']=$value['res_inventory'];//剩余活动产品数量
				//获取子主题，父主题
				$allpnames=$activity->where('pid=%d',0)->getField('id,parenttheme,pid'); //获取所有父主题
				if (!empty($allpnames))
				{
					$tempselect.='<select name="ptheme" class="ptheme">';
					foreach( $allpnames as $k=>$v)
					{
						if ($value['pid'] == $k) 
						{
							$tempselect.="<option value=$k  selected >".$v['parenttheme'].'</option>';
						}else{
							$tempselect.="<option value=$k   >".$v['parenttheme'].'</option>';
						}	
					}
					$tempselect.='</select>';
	
				}

				$result[$key]['pname']=$tempselect;

				$childtheme=$activity->where('id=%d',$value['cid'])->getField('childtheme');
				$cnameselect.='<select name="ctheme" class="ctheme" ><option selected="selected" value='.$value['cid'] .'>'.$childtheme.'</option>';
				$result[$key]['cname']=$cnameselect;
				//获取此产品名称，型号
				$result[$key]['itemprop']=$item->where('itemid=%d',$value['itemid'])->getField('itemname');//产品名称
				$result[$key]['itemprop'].=$item->where('itemid=%d',$value['itemid'])->getField('bn');   //产品名称+型号
				
				//判断此产品是否有规格
				if (!empty($value['productid'])) 
				{
					$result[$key]['itemprop'].=$product->where('productid=%d',$value['productid'])->getField('color');
				}

			}
		}

		return $result;

 	}
 	/*
 	 *总公司添加活动产品时搜索
 	 *@param keyworlds
 	 *return array
 	 */
 	public function searchAllItem($arg){
 		$result='';
 		//只能搜索总公司的产品
 		$keywords=!empty($arg['keywords'])?trim($arg['keywords']):'';
 		$page=!empty($arg['page'])?$arg['page']:1;
 		//$condition='platid != 0 '; //是否是平台产品
 		$condition=' 1=1 '; //是否是平台产品
 		

 		if(!empty($keywords))
 		{
 			$condition.=" AND a.itemname like '%{$keywords}%' OR a.bn like '%{$keywords}%' "; 
 		}

 		$item=M('item a  ');
 		$count=$item->where($condition)->count();

 		//$pagearr= getPageArr($page,C('PAGESIZE'),$count);
 		$pagearr= getPageArr($page,3,$count);

 		$temp=$item->join('inner join '.C('DB_PREFIX').'product b  on a.itemid=b.itemid ')->where($condition)
						 ->order('a.itemid desc')
						 ->limit($pagearr['start'],$pagearr['pagesize'])
						 ->getField('b.productid,a.itemid,a.itemname,a.bn,a.typeid,b.color');
		if(!empty($temp))
		{
			$type=M('type');
			foreach ($temp as $key => &$value) 
			{
				$value['typename']=$type->where('typeid=%d',$value['typeid'])->getField('typename');
			}
		}	
		
		$result['items']=!empty($temp)?array_values($temp):'';
		$result['pagearr']=!empty($pagearr)?$pagearr:'';
		return $result;

 	}

}